package com.easycarebn.main.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.easycarebn.main.utils.IResult;
import com.easycarebn.main.utils.Utils;
import com.easycarebn.request.controller.ServerController;
import com.easycarebn.request.data.ContactInfoResponse;
import com.easycarebn.request.data.DataItemResponse;
import com.easycarebn.request.data.StatusCode;
import com.easycarebn.service.request.OnRequest;
import com.timbacsi.main.MainActivity;
import com.timbacsi.main.R;

public class FragmentContactus extends Fragment {

    private View mView;
    private static FragmentContactus fragment = null;
    private IResult callIResult;
    private static String mTitle;
    private TextView tvEmail;
    private TextView tvPhone;
    private TextView tvAddress;
    private ContactInfoResponse data;

    public static FragmentContactus newInstance(String title) {
        if (fragment == null)
            fragment = new FragmentContactus();
        mTitle = title;
        return fragment;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof IResult) {
            callIResult = (IResult) activity;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_lienhe, null);

        return mView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        callIResult.onDisplay(Utils.TASK_CHILD, "");
        Utils.setTitlePage(mView, mTitle);
        Utils.setOnTouchHideKeyboard(getActivity(), (ViewGroup) mView);
        final EditText edHoten = (EditText) mView.findViewById(R.id.edHoten);
        final EditText edEmailLh = (EditText) mView.findViewById(R.id.edEmailLh);
        final EditText edNoidung = (EditText) mView.findViewById(R.id.edNoidung);
        tvEmail = (TextView) mView.findViewById(R.id.tvCopyRight);
        tvPhone = (TextView) mView.findViewById(R.id.tvPhone);
        tvAddress = (TextView) mView.findViewById(R.id.tvAddress);

        tvPhone.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Utils.callPhone(tvPhone.getText().toString().trim(), getActivity());

            }
        });

        Button btnGui = (Button) mView.findViewById(R.id.btnSend);
        btnGui.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                String mHoten = edHoten.getText().toString().trim();

                if (mHoten.length() == 0) {
                    Utils.showMessage(getActivity(), getResources().getString(R.string.nhap_ho_ten));
                    edHoten.setFocusable(true);
                    return;
                }
                String mEmail = edEmailLh.getText().toString().trim();
                if (mEmail.length() == 0) {
                    Utils.showMessage(getActivity(), getResources().getString(R.string.nhap_email));
                    edEmailLh.setFocusable(true);
                    return;
                }
                if (!Utils.validate(mEmail, Utils.EMAIL_PATTERN)) {
                    Utils.showMessage(getActivity(), getResources().getString(R.string.nhap_email_notmarth));
                    edEmailLh.setFocusable(true);
                    return;
                }

                String mNoidung = edNoidung.getText().toString().trim();
                if (mNoidung.length() == 0) {
                    Utils.showMessage(getActivity(), getResources().getString(R.string.nhap_noidung));
                    edNoidung.setFocusable(true);
                    return;
                }
                /*
                 * if (mNoidung.length() < 20) { Utils.showMessage(
                 * getActivity(), getResources().getString(
                 * R.string.nhap_noidung_notmarth));
                 * edNoidung.setFocusable(true); return; }
                 */
                MainActivity.mLoadingData.setVisibility(View.VISIBLE);
                Utils.disableEnableControls(false, (ViewGroup) mView);

                ServerController.getInstance(getActivity()).requestSendContact(mEmail, mHoten, mNoidung,
                        new OnRequest<DataItemResponse>() {

                            @Override
                            public void onSuccess(DataItemResponse data) {
                                Log.d("onSuccess", data.getCode() + "");
                                MainActivity.mLoadingData.setVisibility(View.GONE);
                                Utils.disableEnableControls(true, (ViewGroup) mView);
                                if (data.getCode() == StatusCode.OK) {
                                    Utils.showMessage(getActivity(), getResources().getString(R.string.gui_thanh_cong));
                                } else {
                                    Utils.showMessage(getActivity(), getResources().getString(R.string.thu_lai_sau));
                                }

                            }

                            @Override
                            public void onError(int errorCode, String message) {
                                // TODO Auto-generated method stub

                            }
                        });
            }
        });
        getContactInfo();

    }

    public void getContactInfo() {
        data = Utils.getContactInfo(getActivity());
        if (data == null) {
            MainActivity.mLoadingData.setVisibility(View.VISIBLE);
            ServerController.getInstance(getActivity()).requestGetContact(new OnRequest<ContactInfoResponse>() {

                @Override
                public void onSuccess(ContactInfoResponse data) {
                    MainActivity.mLoadingData.setVisibility(View.GONE);

                    if (data == null ||data.getContactInfo()==null) {
                        return;
                    }
                    if (data.getCode() == StatusCode.OK) {
                        setContactView(data);
                    }
                    Utils.updateData(getActivity(), Utils.KEY_CONTACT, data);
                }

                @Override
                public void onError(int errorCode, String message) {
                    MainActivity.mLoadingData.setVisibility(View.GONE);

                }
            });
        } else {
            setContactView(data);
        }
    }

    public ContactInfoResponse getContactInfoResponse() {
        return data;
    }

    public void setContactView(ContactInfoResponse data) {
        if(data.getContactInfo()==null)
            return;
        
        tvPhone.setText(Html.fromHtml(data.getContactInfo().getEasycare_phone()));
        tvEmail.setText(Html.fromHtml(data.getContactInfo().getEasycare_email()));
        tvAddress.setText(Html.fromHtml(data.getContactInfo().getEasycare_address()));
    }
}
